import com.sun.corba.se.spi.monitoring.LongMonitoredAttributeBase; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; public class Keno implements ActionListener { JFrame window; JPanel jp; ArrayList buttons; private int count = 0; ArrayList selectedButtons; JButton play; int match = 0; int pay = 0; public void creatWindow(){ window = new JFrame(); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setTitle("KENO"); JPanel panel = new JPanel(); jp = new JPanel( ); buttons = new ArrayList<>(); selectedButtons = new ArrayList<>(); Dimension btnSize = new Dimension(100, 50); GridLayout jp_layout = new GridLayout(11,8); jp.setLayout(jp_layout); GridLayout label_layout = new GridLayout(2,3,10,10); JPanel label_panel = new JPanel(); label_panel.setLayout(label_layout); JTextField bet = new JTextField(); play = new JButton("Play"); play.setEnabled(false); JButton reset = new JButton("Reset/Clear"); /*we use a loop to create all the 84 buttons * each loop create a button, we set a text to the button * we then add the button to the arrayList*/ for(int x = 1; x <= 80; x++){ //create a button JButton btn = new JButton(); //set text to the button btn.setText(""+x); //we set width and hight of the button btn.setBackground(Color.YELLOW); //SET button size btn.setSize(btnSize); //Register action listener btn.addActionListener(this); //add button to array buttons.add(btn); //add button to jpanel jp.add(btn); } /*Add Lable panel at the last row*/ panel.add(jp); /*First row of JPanel*/ JLabel bet_text = new JLabel("BET:"); bet_text.setSize(120,120); label_panel.add(bet_text); label_panel.add(bet); JLabel bet_pay = new JLabel("___PAY__"); label_panel.add(bet_pay); /*Second row of JPanel*/ label_panel.add(play); label_panel.add(reset); JLabel bet_result = new JLabel("___RESULT__"); label_panel.add(bet_result); panel.add(label_panel); /*Add actionListener to play button*/ play.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String user_bet = bet.getText().toString(); if(!user_bet.isEmpty()){ try{ int bet_value = Integer.parseInt(user_bet); if(bet_value >= 1 && bet_value <= 100){ int numbers[] = generateNumbers(); //change the background colours of those buttons selected by //computer for(int x = 0; x < numbers.length; x++){ changeCompSelected(numbers[x]); } //check if user selected buttons match generated numbers for(int x = 0; x < selectedButtons.size(); x++){ checkMatch(selectedButtons.get(x),numbers); } play.setEnabled(false); bet_result.setText("You Matched "+match+" Numbers "); if(match == 0){ pay = bet_value * 5; }else if(match > 0 && match < 5){ pay = bet_value * 0; }else if(match == 5){ pay = bet_value * 2; }else if(match == 6){ pay = bet_value * 5; }else if(match == 7){ pay = bet_value * 10; }else if(match == 8){ pay = bet_value * 12; }else if(match == 9){ pay = bet_value * 15; }else if(match == 6){ pay = bet_value * 10; } bet_pay.setText("PAY $"+pay); }else{ JOptionPane.showMessageDialog(window,"Bet Value Should Be Between 1 and 100"); } }catch(Exception exc){ JOptionPane.showMessageDialog(window,"Invalid Bet Value"); } }else{ JOptionPane.showMessageDialog(window,"Please Place Ypur Bet Before Playing"); } } }); reset.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { ResetAll(); count =0; match = 0; selectedButtons = new ArrayList<>(); pay = 0; bet_pay.setText("__PAY__"); bet_result.setText("__RESULT__"); } }); //add panel to frame window.setSize(500,500); window.add(panel); window.setVisible(true); } /** * Main method * @param args */ public static void main(String[] args){ Keno ken = new Keno(); ken.creatWindow(); } /** * Actio for buttons * @param e */ @Override public void actionPerformed(ActionEvent e) { JButton btn = (JButton)e.getSource(); btn.setBackground(Color.blue); if(checkAndRemoveButton(btn)){ selectedButtons.add(btn); count++; } if(count == 10){ disableAll(); play.setEnabled(true); }else { EnableAll(); } } public boolean checkAndRemoveButton(JButton btn){ for(int x =0; x < selectedButtons.size(); x++){ if(selectedButtons.get(x).getText().equalsIgnoreCase(btn.getText())){ selectedButtons.get(x).setBackground(Color.YELLOW); selectedButtons.remove(x); count--; return false; } } return true; } public boolean checkButton(JButton btn){ for(int x =0; x < selectedButtons.size(); x++){ if(selectedButtons.get(x).getText().equalsIgnoreCase(btn.getText())){ return true; } } return false; } public void disableAll(){ for(int x = 0; x < buttons.size(); x++){ if(!checkButton(buttons.get(x))){ buttons.get(x).setEnabled(false); } } play.setEnabled(false); } public void EnableAll(){ play.setEnabled(false); for(int x = 0; x < buttons.size(); x++){ buttons.get(x).setEnabled(true); } } public void ResetAll(){ play.setEnabled(false); for(int x = 0; x < buttons.size(); x++){ buttons.get(x).setEnabled(true); buttons.get(x).setBackground(Color.YELLOW); } } public int[] generateNumbers(){ int numbers[] = new int[10]; for(int x = 0; x < 10 ; x++){ int rand = (int)(Math.random() *((80 - 1) + 1)) + 1; System.out.println(" ->> "+rand); numbers[x] = rand; } return numbers; } public void checkMatch(JButton button,int numbers[]){ for(int x = 0; x < numbers.length; x++){ int btnValue = Integer.parseInt(button.getText()); if(numbers[x] == btnValue){ System.out.println("Found...."+btnValue); match++; button.setBackground(Color.GREEN); return; } } button.setBackground(Color.RED); } public void changeCompSelected(int number){ for(int x =0; x < buttons.size(); x++){ int btnValue = Integer.parseInt(buttons.get(x).getText()); if(number == btnValue){ buttons.get(x).setBackground(Color.WHITE); } } } }